/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servermba;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.sql.Date;
import servermba.mdb.ApiManeger;
import servermba.object.CreditCard;
import servermba.object.ResultReponse;
import servermba.object.SavingAccount;

/**
 *
 * @author lekhanhduy
 */
public class ServerThread extends Thread {

    private Socket socket;
    private BufferedReader in;
    private PrintWriter out;
    private ApiManeger apiManager;
    private Gson gson;

    public ServerThread(Socket s) throws IOException {
        apiManager = new ApiManeger();
        socket = s;
        System.out.println("Serving: " + socket);
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
                socket.getOutputStream())), true);
        gson = new GsonBuilder().serializeNulls().create();
    }

    @Override
    public void run() {
        try {
            while (true) {
                System.out.println("....Server is waiting...");
                String str = in.readLine();
                System.out.println("Received: " + str);
                System.out.println("From: " + socket);

                String reponse = null;
                if(str.contains("loginSavingAccount"))
                {
                    String param[] = str.split("/");
                    if (param.length == 2) {
                        reponse = String.valueOf(loginSavingAccount(param[1]));
                    }
                }
                else if (str.contains("login")) {
                    String param[] = str.split("/");
                    if (param.length == 3) {
                        reponse = String.valueOf(login(param[1], param[2]));
                    }
                }
                else if (str.contains("getCreditCardOfUser")) {
                    String param[] = str.split("/");
                    CreditCard creditCard = getCreditCardOfUser(param[1]);
                    reponse = gson.toJson(creditCard);
                } else if (str.contains("sentWithDrawMoney")) {
                    String param[] = str.split("/");
                    reponse = sentWithDrawMoney(param[1], param[2], Double.valueOf(param[3]));
                } else if (str.contains("check_transfers")) {
                    String param[] = str.split("/");
                    reponse = gson.toJson(check_transfers(param[1]));
                } else if (str.contains("transfers_creditcard")) {
                    String param[] = str.split("/");
                    reponse = gson.toJson(transfers_creditcard(param[1], param[2], Double.valueOf(param[3])));
                } else if (str.contains("getAllHistoryCreditCard")) {
                    String param[] = str.split("/");
                    reponse = getAllHistoryCreditCard(param[1], Date.valueOf(param[2]), Date.valueOf(param[3]));
                }else if(str.contains("changePassCreditCard"))
                {
                    String param[] = str.split("/");
                    reponse = gson.toJson(changePassCreditCard(param[1], param[2]));
                }else if(str.contains("getAllUserCreditCard"))
                {
                    reponse = getAllUserCreditCard();
                }else if(str.contains("getInfoSavingAccount"))
                {
                    String param[] = str.split("/");
                    SavingAccount savingAccount = getInfoSavingAccount(param[1]);
                    reponse = gson.toJson(savingAccount);
                }else if(str.contains("withDrawMoneySavingAccount"))
                {
                     String param[] = str.split("/");
                     reponse = gson.toJson(withDrawMoneySavingAccount(param[1], Double.parseDouble(param[2])));
                }else if(str.contains("getAllHistorySavingAccount"))
                {
                    String param[] = str.split("/");
                    reponse = getAllHistorySavingAccount(param[1], Date.valueOf(param[2]), Date.valueOf(param[3]));
                }else if(str.contains("caculInterest"))
                {
                     String param[] = str.split("/");
                     reponse = gson.toJson(caculInterest(param[1], Date.valueOf(param[2])));
                }
                // gui lai cho client
                out.println(reponse);
                out.flush();
                break;
            }
            System.out.println("Disconnected with.." + socket);
        } catch (IOException e) {
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
            }
        }
    }

    public CreditCard getCreditCardOfUser(String accountNumber) {
        return apiManager.getCreditCardOfUser(accountNumber);
    }

    public SavingAccount getInfoSavingAccount(String accountNumber)
    {
        return  apiManager.getInfoSavingAccount(accountNumber);
    }
    
    public boolean login(String accountNumber, String pass) {
        return apiManager.login(accountNumber, pass);
    }

    public boolean loginSavingAccount(String accountNumber)
    {
        return apiManager.loginSavingAccount(accountNumber);
    }
    
    public String sentWithDrawMoney(String accountCode, String method, double money) {
        return apiManager.sentWithDrawMoney(accountCode, method, money);
    }

    public ResultReponse check_transfers(String user) {
        ResultReponse result = new ResultReponse();
        boolean temp = apiManager.checkTransfersCreditCard(user);
        result.setSuccess(temp);
        if (temp) {
            result.setNote("Tài khoản hợp lệ..!");
        } else {
            result.setNote("Tài khoản bạn muốn chuyển không hợp lệ!");
        }
        return result;
    }

    public ResultReponse transfers_creditcard(String userSent, String userRev, double money) {
        return apiManager.transfers_creditcard(userSent, userRev, money);
    }

    public String getAllHistoryCreditCard(String account, Date fromDate, Date toDate) {
        return apiManager.getAllHistory(account, fromDate, toDate);
    }
    
    public ResultReponse changePassCreditCard(String account,String pass)
    {
       return apiManager.changePassCreditCard(account, pass);
    }
    
    public String getAllUserCreditCard()
    {
        return apiManager.getAllUserCreditCard();
    }
    
    public ResultReponse withDrawMoneySavingAccount(String accountNumber,double  money)
    {
        return apiManager.withDrawMoneySavingAccount(accountNumber, money);
    }
    
    public String getAllHistorySavingAccount(String accountNumber, Date fromDate, Date toDate)
    {
        return  apiManager.getAllHistorySavingAccount(accountNumber, fromDate, toDate);
    }
    
    public ResultReponse caculInterest(String accountNumber, Date date)
    {
        return apiManager.caculInterest(accountNumber, date);
    }
}
